﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


//TVERSION=1
//MOTDL=6
//MOTD=This is a simple tracker
//MOTD=Tracker Statistics
//MOTD=Number of Tracked Games: 8431
//MOTD=Number of Tracker Game List Requests: 29527
//MOTD=Number of Tracker Interesting Game List Requests: 285
//MOTD=Number of Tracker Web Requests: 466
//NGAMES=3

//GAME000=winbolo.yesacrossamerica.com:27501
//VERSION=1.16
//MAP=Chew Toy 3
//TYPE=Strict
//PLAYERS=0
//BASES=16
//PILLS=16
//HIDMINES=No
//PASSWORD=No
//BRAINS=yesAdv
//DELAY=0
//LIMIT=-1
//STARTTIME=1238247629

namespace WinBolo_Tracker
{
    public class Parser
    {
        static string GameFormat(int num)
        {
            string result = num.ToString();

            int len = result.Length;

            for (int i = len; i < 3; i++)
                result = "0" + result;

            return result;
        }
        //"Forest Rig - winbolo.yesacrossamerica.com:27500"
        // "\nType:    Open<br>Locked:  No<br>Version: 1.16<br>Players: <br>Neutral Bases: 16<br>Neutral Pills: 16<br>\t\t\t"
        // Parsing RSS feed
        public static Game Parse(string title, string description)
        {
            Game game = new Game();

            string[] tokens1 = title.Split(new string[] { " - ", ":" }, StringSplitOptions.RemoveEmptyEntries);

            game.Map = tokens1[0].Trim();
            game.Address = tokens1[1].Trim();
            game.Port = int.Parse(tokens1[2].Trim());

            string[] tokens2 = description.Split(new string[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries );

            game.Locked = tokens2[1].Split(new char[] { ':' })[1].Trim().Equals("Yes");

            string[] players = tokens2[3].Split(new char[] { ':' })[1].Split(new char[] { '+' });

            foreach (string player in players)
                game.PlayerList.Add(new Player(Uri.UnescapeDataString(player.Trim())));

            return game;
        }

        // Parsing winbolo tracker
        public static Tracker Parse(string data)
        {
            if (data == "")
                return null;

            Tracker tracker = new Tracker();

            try
            {
                int NGAMESPOS = data.IndexOf("NGAMES");
                if (NGAMESPOS == -1) return null;

                int GAME_SPOS = data.IndexOf("GAME", NGAMESPOS + 2);

                tracker.NGames = int.Parse(data.Substring(NGAMESPOS + 7, GAME_SPOS - 2 - (NGAMESPOS + 7)));
                tracker.Games = new Game[tracker.NGames];
                
                for (int x = 0; x < tracker.NGames; x++)
                {
                    tracker.Games[x] = new Game();

                    GAME_SPOS = data.IndexOf("GAME" + GameFormat(x));
                    int VERSION_SPOS = data.IndexOf("VERSION", GAME_SPOS);
                    tracker.Games[x].Address = data.Substring(GAME_SPOS + 8, VERSION_SPOS - 2 - (GAME_SPOS + 8));

                    tracker.Games[x].Port = int.Parse(tracker.Games[x].Address.Substring(tracker.Games[x].Address.IndexOf(":") + 1));
                    tracker.Games[x].Address = tracker.Games[x].Address.Substring(0, tracker.Games[x].Address.IndexOf(":"));

                    int MAP_SPOS = data.IndexOf("MAP", VERSION_SPOS);
                    tracker.Games[x].Version = data.Substring(VERSION_SPOS + 8, MAP_SPOS - 2 - (VERSION_SPOS + 8));

                    int TYPE_SPOS = data.IndexOf("TYPE", MAP_SPOS);
                    tracker.Games[x].Map = data.Substring(MAP_SPOS + 4, TYPE_SPOS - 2 - (MAP_SPOS + 4));

                    int PLAYERS_SPOS = data.IndexOf("PLAYERS", TYPE_SPOS);
                    tracker.Games[x].Type = data.Substring(TYPE_SPOS + 5, PLAYERS_SPOS - 2 - (TYPE_SPOS + 5));

                    int BASES_SPOS = data.IndexOf("BASES", PLAYERS_SPOS);
                    tracker.Games[x].Players = int.Parse(data.Substring(PLAYERS_SPOS + 8, BASES_SPOS - 2 - (PLAYERS_SPOS + 8)));

                    int PILLS_SPOS = data.IndexOf("PILLS", BASES_SPOS);
                    tracker.Games[x].Bases = int.Parse(data.Substring(BASES_SPOS + 6, PILLS_SPOS - 2 - (BASES_SPOS + 6)));

                    int HIDMINES_SPOS = data.IndexOf("HIDMINES", PILLS_SPOS);
                    tracker.Games[x].Pills = int.Parse(data.Substring(PILLS_SPOS + 6, HIDMINES_SPOS - 2 - (PILLS_SPOS + 6)));

                    int STARTTIME_SPOS = data.IndexOf("STARTTIME", HIDMINES_SPOS);
                    int NEXTGAME_SPOS = data.IndexOf("GAME", STARTTIME_SPOS);

                    if (NEXTGAME_SPOS == -1)
                        tracker.Games[x].StartTime = data.Substring(STARTTIME_SPOS + 10, 10);
                    else
                        tracker.Games[x].StartTime = data.Substring(STARTTIME_SPOS + 10, NEXTGAME_SPOS - 2 - (STARTTIME_SPOS + 10));

                    tracker.Games[x].TrackTime = DateTime.Now;
                }
            }
            catch (Exception e)
            {
                Logger.Log(e.Message);
            }

            return tracker;
        }
    }
}
